/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package admin;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author
 */
@WebServlet(name = "FileUpload", urlPatterns = {"/admin/FileUpload"})
public class FileUpload extends HttpServlet {

    public void doPost(HttpServletRequest request,
            HttpServletResponse response) throws IOException, ServletException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

// the directory where the uploaded file will be saved.
        String savePath = "C:\\ITI\\Servlets and jsp\\project\\trunk\\E-comm\\web\\img\\";

        String fileName = "";
        ServletInputStream in = request.getInputStream();
        byte[] line = new byte[128];
        int i = in.readLine(line, 0, 128);
        int boundaryLength = i - 2;
        String boundary = new String(line, 0, boundaryLength); //-2
//discards the newline character
        while (i != -1) {
            String newLine = new String(line, 0, i);
            if (newLine.startsWith("Content-Disposition: form-data; name=\"")) {
                String s = new String(line, 0, i - 2);
                int pos = s.indexOf("filename=\"");
                if (pos != -1) {
                    String filepath = s.substring(pos + 10, s.length() - 1);
                    // Windows browsers include the full path on the client
                    // But Linux/Unix and Mac browsers only send the filename
                    // test if this is from a Windows browser
                    pos = filepath.lastIndexOf("\\");
                    if (pos != -1) {
                        fileName = filepath.substring(pos + 1);
                    } else {
                        fileName = filepath;
                    }
                }
                //this is the file content
                i = in.readLine(line, 0, 128);
                i = in.readLine(line, 0, 128);
                // blank line
                i = in.readLine(line, 0, 128);
                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                newLine = new String(line, 0, i);
                while (i != -1 && !newLine.startsWith(boundary)) {
                    // the problem is the last line of the file content
                    // contains the new line character.
                    // So, we need to check if the current line is
                    // the last line.
                    buffer.write(line, 0, i);
                    i = in.readLine(line, 0, 128);
                    newLine = new String(line, 0, i);
                }
                try {
                    // save the uploaded file
                    RandomAccessFile f = new RandomAccessFile(savePath + fileName, "rw");
                    byte[] bytes = buffer.toByteArray();

                    f.write(bytes, 0, bytes.length - 2);
                    f.close();

                } catch (Exception e) {
                }
            }
            i = in.readLine(line, 0, 128);
        } // end while

        RequestDispatcher dis = getServletContext().getRequestDispatcher("/admin/addProductView.jsp");
        dis.forward(request, response);

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }
}
